import java.util.Iterator;
import java.util.LinkedList;


public class Process 
{

	String ID;
	String Name;
	int arrivalTime;
	LinkedList<Integer> cpuTimes = new LinkedList<Integer>();
	LinkedList<Integer> ioTimes = new LinkedList<Integer>();
	
	
	int totalCPUBurst = 0;
	int totalIOBurst = 0;
	
	public int executedTime;
	
	public boolean hasIO = false;
	public boolean hasCPU = false;
	
	public boolean cpuBurstDone = false;
	public boolean IOBurstDone = false;
	public boolean done = false;
	
	public boolean interrupted = false;
	
	
	public int finishTime;
	public int waitingTime;
	public int turnaroundTime;
	
	public Process(String ID, String Name, String arrivalTime)
	{
		this.ID = ID;
		this.Name = Name;
		this.arrivalTime = Integer.valueOf(arrivalTime);
	}
	
	
	public void update()
	{
		if(hasCPU)
		{
			executedTime++;
			IOBurstDone = false;
			cpuTimes.addFirst(cpuTimes.remove() - 1);
			if(cpuTimes.peek() == 0)
			{
				
				hasCPU = false;
				cpuBurstDone = true;
				hasIO = true;
				cpuTimes.remove();
			}
			else
				cpuBurstDone = false;
			
		}
		else if(hasIO && !ioTimes.isEmpty())
		{
			ioTimes.addFirst(ioTimes.remove() - 1);
			if(ioTimes.peek() == 0)
			{
				hasIO = false;
				IOBurstDone = true;
				ioTimes.remove();
				
			}
			
		}
		if(cpuTimes.isEmpty())
		{
			done = true;
		}
	}
	
	public void calculateTotals()
	{
		Iterator<Integer> itr = cpuTimes.iterator();
		while(itr.hasNext())
		{
			totalCPUBurst += itr.next();
		}
		
		 itr = ioTimes.iterator();
		while(itr.hasNext())
		{
			totalIOBurst += itr.next();
		}
	}
	
	public void calculateStats()
	{
		waitingTime = finishTime - arrivalTime - totalCPUBurst - totalIOBurst;
		turnaroundTime = finishTime - arrivalTime;
	}
}
